/**
 * Created by 荆安喜 on 2017/10/10.
 */
$(function () {
    var ad = appData()
    var paramsT = {}
    $(function () {
        //时间插件
        $.daterRangeDefine("#DateRange", 'YYYY-MM-DD')

        $('#DateRange').val(moment().subtract('days', 30).format('YYYY-MM-DD') + ' - ' + moment().format('YYYY-MM-DD'))

        $('#search').on('click', riskEvent)

        $('#search').click()

        $('.more').on('click', moreTable)

        $(".dialog .close,#cancel").on("click", function () {
            $(".dialog").hide();
        })

        //事件统计
        ad.selectList('KMGA_RISK_STATISTICS', {}, function (data) {
            var names = []
            var series = []
            data.datas.forEach(function (d, i) {
                names.push(d.name)
                var serie = {
                    name: d.name,
                    type: 'line',
                    lineStyle: {
                        normal: {
                            width: 2,
                        }
                    },
                    data: d.data
                }
                series.push(serie)
            })
            var option = {

                tooltip: {
                    trigger: 'axis'
                },

                legend: {
                    data: names
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: data.xAxis
                },
                yAxis: [{
                    type: 'value',
                    boundaryGap: [0, '100%'],
                    splitLine: {
                        show: false
                    }
                }],
                dataZoom: [{
                    type: 'inside',
                    start: 50,
                    end: 100
                }, {
                    show: true,
                    type: 'slider',
                    y: '90%',
                    start: 50,
                    end: 100
                }],
                series: series
            }
            charts('chart4', option)
        })
    })


    //柱状图
    function charts(id, option) {
        var myChart = echarts.init(document.getElementById(id));
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
        return myChart
    }

    var that

    //检索事件
    function riskEvent() {
        var time = $('#DateRange').val()
        var startTime = time.substring(0, 10)
        var endTime = time.substring(13, 23)

        var parms = {
            startTime: startTime,
            endTime: endTime
        }

        //风险级别
        ad.selectList('KMGA_RISK_LEVEL', parms, function (data) {
            var option = {
                title: {
                    text: data.name,
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b}: {c} ({d}%)",
                },
                legend: {
                    // orient: 'vertical',
                    y: 'bottom',
                    itemWidth: 14,
                    itemHeight: 14,
                    align: 'left',
                    data: data.legendData,
                },
                series: [
                    {
                        name: '访问来源',
                        type: 'pie',
                        radius: ['42%', '55%'],
                        selectedMode: 'single',
                        hoverAnimation: true,
                        label: {
                            normal: {
                                formatter: '{b}\n{d}%'
                            },

                        },
                        data: data.datas
                    }
                ]
            }
            var pie = charts('chart1', option)
            var select
            pie.on('click', function (params) {
                if (select == params.dataIndex) {
                    linkage(parms)
                } else {
                    var parm = $.extend({}, parms)
                    parm.type = params.data.id
                    linkage(parm)
                }
                select = params.dataIndex
            })
        })
        function linkage(d) {
            that=d.type
            //风险数据源
            ad.selectList('KMGA_RISK_DATA', d, function (data) {
                var option = {
                    title: {
                        text: data.name
                    },
                    grid: {
                        left: '30%',
                    },
                    yAxis: {
                        data: data.yaxis
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b}: {c}",
                    },
                    xAxis: {},
                    series: [{
                        name: '次数',
                        type: 'bar',
                        barWidth: 20,
                        data: data.data
                    }]
                }
                charts('chart2', option)
            })
            //风险IP
            ad.selectList('KMGA_RISK_IP', d, function (data) {
                var option = {
                    title: {
                        text: data.name
                    },
                    yAxis: {
                        data: data.yaxis,
                    },
                    grid: {
                        left: '30%'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b}: {c}",
                    },
                    xAxis: {},
                    series: [{
                        name: '次数',
                        type: 'bar',
                        barWidth: 20,
                        data: data.data
                    }]
                }
                charts('chart3', option)
            })
        }

        linkage(parms)
    }

    //更多的分页
    function moreTable() {
        $("#more1").show();
        //分页
        if (this.dataset.id == 'data') {
            $('#title').html('风险事件排名-资源')
            showTable(true, 1, 'KMGA_RISK_MOREDATA', 'alarmPage')
        } else {
            $('#title').html('风险事件排名-客户端IP')
            showTable(true, 1, 'KMGA_RISK_MOREIP', 'alarmPage')
        }


    }


    //分页信息
    function showTable(dontRenderPager, page, url, domId) {
        var time = $('#DateRange').val()
        var startTime = time.substring(0, 10)
        var endTime = time.substring(13, 23)
        paramsT.startTime = startTime
        paramsT.endTime = endTime
        paramsT.page = page
        paramsT.pageSize = 10
        paramsT.type=that
        ad.selectList(url, paramsT, function (data) {
            $("#" + domId).empty();
            if (data.content.length === 0) {
                $("#" + domId).append("<tr><td colspan='4'  class='colspanCss'><h1>暂无数据</h1></td></tr>");
                $("#pageList").empty();
                return;
            }

            if (data.content <= appApi("PAGE_SIZE")) {
                $("#pageList").empty();
                return;
            }
            $.get('scripts/app/safe/riskEvents/more.tmpl', function (tmpl) {
                $("#" + domId).empty();
                dataPages = data
                $.tmpl(tmpl, data).appendTo("#" + domId);

                if (dontRenderPager) {
                    renderPager(data.totalRecord,null,url);
                }
            })

        });
    }

    function renderPager(totalRecord, nowPage,url) {
        $("#pageList").pagination(totalRecord, 10, {
            callback: function (page_id, jq) {
                pageNumNow = page_id + 1;
                showTable(false, pageNumNow, url, 'alarmPage');
            }
        }, nowPage);
    }


})